﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 产品管理仓储
/// </summary>
public class MdProductRep : BaseRep<MdProductModel>
{
    private static string sqlFields = string.Format(@"
        `product_id`,
        `title`,
        `description`,
        `quantity`,
        `market_price`,
        `seliing_price`,
        `purchase_link`,
        `content`,
        `illustrate`,
        `picture`,
        `thum_picture`,
        `category`,
        `status`,
        `del_flag`,
        `create_by`,
        `create_time`,
        `update_by`,
        `update_time`,
        `remark`
    ");

    /// <summary>
    /// 通过产品编号获取产品详情
    /// </summary>
    /// <param name="productId">产品编号</param>
    /// <returns>MdProductModel</returns>
    public MdProductModel? GetMdProductById(long productId)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `product_id` = @ProductId ");

        return QuerySingle(sqlFields, sqlWhere, new { ProductId = productId });
    }

    /// <summary>
    /// 获取产品列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒叙</param>
    /// <returns>IEnumerable<MdProductModel></returns>
    public IEnumerable<MdProductModel> GetMdProductList(MdProductModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Title))
        {
            sqlWhere += " and `title` like concat('%', @Title, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Category))
        {
            sqlWhere += " and `category` = @Category ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取产品列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<MdProductModel></returns>
    public Page<MdProductModel> GetMdProductPageList(MdProductModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Title))
        {
            sqlWhere += " and `title` like concat('%', @Title, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Category))
        {
            sqlWhere += " and `category` = @Category ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过产品编号更新产品
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateMdProductById(MdProductModel model)
    {
        var sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.Title))
        {
            sqlSet += " `title` = @Title, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Description))
        {
            sqlSet += " `description` = @Description, ";
        }

        if (model.Quantity > -1)
        {
            sqlSet += " `quantity` = @Quantity, ";
        }

        if (!string.IsNullOrWhiteSpace(model.MarketPrice))
        {
            sqlSet += " `market_price` = @MarketPrice, ";
        }

        if (!string.IsNullOrWhiteSpace(model.SeliingPrice))
        {
            sqlSet += " `seliing_price` = @SeliingPrice, ";
        }

        if (!string.IsNullOrWhiteSpace(model.PurchaseLink))
        {
            sqlSet += " `purchase_link` = @PurchaseLink, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Content))
        {
            sqlSet += " `content` = @Content, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Illustrate))
        {
            sqlSet += " `illustrate` = @Illustrate, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Picture))
        {
            sqlSet += " `picture` = @Picture, ";
        }

        if (!string.IsNullOrWhiteSpace(model.ThumPicture))
        {
            sqlSet += " `thum_picture` = @ThumPicture, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Category))
        {
            sqlSet += " `category` = @Category, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `del_flag` = '0' and `product_id` = @ProductId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过产品编号删除产品
    /// </summary>
    /// <param name="ids">产品编号多个逗号分隔</param>
    /// <returns>int</returns>
    public int DeleteMdProductById(string ids)
    {
        string sqlWhere = " and `product_id` in @ProductIds ";

        return Delete(sqlWhere, new { ProductIds = ids.Split(',').ToArray() });
    }
}

